package com.javaee.term.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaee.term.exception.HandException;
import com.javaee.term.form.CustomerForm;
import com.javaee.term.model.Contact;
import com.javaee.term.model.Customer;
import com.javaee.term.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.javaee.term.utils.KeyUtil;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * author 陈伯瑞
 * date 2019/12/16 0016 23:23
 * version 1.0
 **/
@Controller
@RequestMapping("/seller/customer")
@Slf4j
public class CustomerController {
    @Autowired
    private  CustomerService customerService;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {

        PageHelper.startPage(page,size);
        List<Customer> list=customerService.findAll();
        PageInfo<Customer> CustomersInfoPage=new PageInfo<Customer>(list);
        map.put("CustomersInfoPage", CustomersInfoPage);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("totalpage", CustomersInfoPage.getPages());
        return new ModelAndView("customer/list", map);
    }
    @GetMapping("/delete")
    public ModelAndView list(@RequestParam(value = "customerId") Integer customerid,
                             Map<String, Object> map) {
        try {
            customerService.delete(customerid);
        } catch (HandException e) {
            log.error("【后台端删除用户】发生异常{}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/customer/list");
            return new ModelAndView("common/error", map);
        }

        map.put("msg", "操作成功");
        map.put("url", "/sell/seller/customer/list");
        return new ModelAndView("common/success",map);
    }
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "customerId", required = false) String customerId,
                              Map<String, Object> map) {
        if (!StringUtils.isEmpty(customerId)) {
            Customer  customerInfo = customerService.findOne(customerId);
            map.put("customerInfo", customerInfo);
        }

        return new ModelAndView("customer/index", map);
    }
    @PostMapping("/save")
    public ModelAndView save(@Valid CustomerForm form,
                             BindingResult bindingResult,
                             Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/customer/index");
            return new ModelAndView("common/error", map);
        }

        Customer customer = new Customer();
        try {
            //如果customerId为空, 说明是新增
            if (!StringUtils.isEmpty(form.getCustomerId())) {
                customer = customerService.findOne(form.getCustomerId());
                BeanUtils.copyProperties(form, customer);
                customerService.update(customer);
            } else {
                form.setCustomerId(KeyUtil.genUniqueKey());
                BeanUtils.copyProperties(form, customer);
                customerService.insert(customer);
            }
        } catch (HandException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/customer/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/sell/seller/customer/list");
        return new ModelAndView("common/success", map);
    }

}
